--[[
文件名: init_reactor.lua
描述: 初始化反应堆
作者: 671643387@qq.com
创建日期: 2025-09-20
]]

local serialization = require("serialization")
local oc_util = require('kernel.util.oc_util')
local device_util = require('kernel.util.device_util')
local FluidReactor = require('fluid_reactor.fluid_reactor')

-- 初始化反应堆
-- @param filename  配置文件名字
local init_reactor = function(filename)
    -- 创建反应堆数据结构
    local reactor = FluidReactor:new()
    -- 初始化物品访问设备
    local item_port_address = device_util.choose_device('Select item transposer:', 'transposer')
    if not reactor:init_item_transposer(item_port_address) then
        print('Reactor access hatch initialize failed')
        return nil
    end
    -- 初始化红石设备
    local red_stone_port_address = device_util.choose_device('Select redstone:', 'redstone')
    reactor:init_redstone(red_stone_port_address)
    -- 初始化me访问设备
    local me_port_address = device_util.choose_device('Select me:', 'me')
    if not reactor:init_me_transposer(me_port_address) then
        print('Reactor me access port initialize failed')
        return nil
    end
    -- 生成配置信息
    local config = reactor:gen_config();
    -- 保存配置文件
    oc_util.save_file(filename, serialization.serialize(config))
    -- 打印调试信息
    reactor:dump()
    -- 返回初始化好的反应堆结构
    return reactor
end

return init_reactor
